home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7658 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  45 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.netins.net!isac!gg
  3. From: gg@isac.hces.com (Greg Goodrich)
  4. Subject: Re: Q: How to assign structures?
  5. Message-ID: <1996Feb27.234658.3537@isac.hces.com>
  6. Organization: Health Care Expert Systems
  7. X-Newsreader: TIN [version 1.2 PL2]
  8. References: <4gsalu$dll@ccshst05.cs.uoguelph.ca>
  9. Date: Tue, 27 Feb 1996 23:46:58 GMT
  10.  
  11. Toby K Hay (thay@uoguelph.ca) wrote:
  12. : I need to assign values to structures in one array from structures in 
  13. : another array - in essence I'm copying the whole structure from one array 
  14. : to the other.  Can I use simple assignment like this:
  15.  
  16. : struct StructName {
  17. :     int     IVal;
  18. :     float    FVal;}
  19.  
  20. : struct StructName Array1[5], Array2[5];
  21. : . . .
  22. : for (i=0;i<5;i++) Array1[i] = Array2[i];
  23. : . . .
  24.  
  25. structures in C can be copied by simple assignment as in:
  26.  
  27.     struct1 = struct2;
  28.  
  29. : And if not that, can I use memcpy() and sizeof() to do the assignment:
  30.  
  31. : for (i=0;i<5;i++) memcpy(&Array2[i],&Array1[i],sizeof(struct StructName));
  32. : Or do I have to assign each element of the structure?
  33.  
  34. You can use memcpy to copy structure data, but as I mentioned above, you
  35. don't need to.  However, make sure that you proceed with caution when
  36. your structures contain pointers, because you may create an undesired
  37. side effect by copying a pointer value from one struct to the next.
  38.  
  39. Greg.
  40. -- 
  41. _______________________________________
  42.     Greg Goodrich - gg@hces.com
  43.     Software Engineer
  44.     PACE Health Management Systems
  45.